package cn.hutool.osc.utils;

import cn.hutool.osc.config.OscConfig;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;

import java.net.URI;

public class S3Utils {

    public S3Client s3Client;

    private final String GbucketName;
    private final String Gregion;

    public S3Utils(OscConfig config, S3ClientBuilder builder) {
        Region region = Region.of(config.getRegion());
        AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
                config.getAccessKey(), // AK，即Access Key ID
                config.getSecretKey() // SK，即Secret Access Key
        );
        URI endpointuri = URI.create(config.getEndpoint());
        s3Client = builder.region(region)
                .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                .endpointOverride(endpointuri)
                .build();
        GbucketName = config.getBucketName();
        Gregion = config.getRegion();
    }

    public void shutdown() {
        s3Client.close();
    }
}
